package com.greate.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.greate.community.dao.SchoolMapper;
import com.greate.community.entity.School;
import com.greate.community.service.SchoolService;
import com.greate.community.util.BusCodeEnum;
import com.greate.community.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService  {

    @Autowired(required = false)
    private SchoolMapper mapper;

    @Override
    public List<School> getSchoolByCity(Long cityId) {
        if (cityId == 0){
            throw new BusinessException(BusCodeEnum.MISSING_CONTENT);
        }
        LambdaQueryWrapper<School> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(School::getCityId, cityId);
        return mapper.selectList(queryWrapper);
    }

    @Override
    public List<School> getAllSchools() {
        return mapper.selectList(null);
    }

    @Override
    public List<School> getSchoolByName(String schoolName) {
        LambdaQueryWrapper<School> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(School::getName, schoolName);
        return mapper.selectList(wrapper);
    }

    @Override
    public School getSchoolById(Long schoolId) {
        return mapper.selectById(schoolId);
    }
}
